package view;

import javax.swing.JFrame;
import javax.swing.JTextField;
/**
 * Class is responsible for text field where logger depth will be entered.
 */
class IntTextField extends JTextField
{
	/**
	 * 
	 */
	private static final long serialVersionUID = 5011193916808774752L;

	/**
	 * C'tor
	 * @param defval - defult value for the text field
	 * @param size - the text field size
	 */
	public IntTextField(String defval, int size)
	{
		super("" + defval, size);
	}
 
	/**
	 * This method checks that the natural number value was entered to the text field.
	 * @param N/A
	 * @return 	true - if text in the text field is the natural number
	 * 			false - in other case
	 */
	public boolean isValid()
	{
		try
		{
			Integer.parseInt(getText());
			return true;
		} catch (NumberFormatException e) {
			ErrorFrame.AddError("You need to enter natural number", new JFrame());
	    	return false;
	    }
	  }

	/**
	 * This method gets the natural number value that was entered to the text field.
	 * @param N/A
	 * @return the natural number value that was entered to the text field.
	 * 			or the default value if theileegal string was entered to the text field
	 */
	  public int getValue() {
	    try 
	    {
	      return Integer.parseInt(getText());
	    } catch (NumberFormatException e) {
	    	ErrorFrame.AddError("You need to enter natural number", new JFrame());
			throw new NumberFormatException();
	    }
	  }

	public void setText(int value)
	{
		setText(new Integer(value).toString());
	}
}
